package com.nanbei.eduService.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanbei.commonUntil.R;
import com.nanbei.eduService.entity.EduCourse;
import com.nanbei.eduService.entity.EduTeacher;
import com.nanbei.eduService.service.EduCourseService;
import com.nanbei.eduService.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "前端讲师管理")
@RestController
@RequestMapping("/eduService/frontTeacher")
@CrossOrigin  //解决跨域问题
public class TeacherFrontController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService eduCourseService;

    //分页查询讲师的方法
    @ApiOperation(value = "分页查询讲师）")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        //创建page对象
        Page<EduTeacher> pageTeachers = new Page<>(current,limit);

        //调用分页方法
        //调用方法的时候，底层封装，把所有的数据封装到pageTeacher对象里面
        teacherService.page(pageTeachers, null);

        //返回总记录数
        long total = pageTeachers.getTotal();
        //数据List集合
        List<EduTeacher> records = pageTeachers.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 根据讲师ID查询讲师信息和讲师课程
     * @param teacherId
     * @return
     */
    @GetMapping("/findTeacherCourseByTeacherById/{teacherId}")
    public R findTeacherCourseByTeacherById(@PathVariable String teacherId){

        //查新讲师个人信息
        EduTeacher teacher = teacherService.getById(teacherId);

        //查询讲师课程
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.eq("teacher_id",teacherId);
        List<EduCourse> courseList = eduCourseService.list(eduCourseQueryWrapper);

        return R.ok().data("teacher",teacher).data("courseList",courseList);

    }
}
